package com.gzk.org.service.impl;

import com.gzk.basic.service.impl.BaseServiceImpl;
import com.gzk.basic.util.MD5Utils;
import com.gzk.basic.util.PageList;
import com.gzk.basic.util.StrUtils;
import com.gzk.org.domain.Employee;
import com.gzk.org.mapper.EmployeeMapper;
import com.gzk.org.query.EmployeeQuery;
import com.gzk.org.service.IEmployeeService;
import com.gzk.user.domain.Logininfo;
import com.gzk.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void add(Employee employee) {

        //初始化employee
        initEmplyoee(employee);
        //先存loginifo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        logininfoMapper.save(logininfo);
        employee.setLogininfo(logininfo);

        employeeMapper.save(employee);
    }

    private void initEmplyoee(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        //加密加盐
        String password = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(password);
    }
}
